<template>
  <div class="app-container calendar-list-container">
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit >

      <el-table-column min-width="200" align="center" :label="$t('course.name')">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="200" align="center" :label="$t('course.theme')">
        <template slot-scope="scope">
          <span>{{ scope.row.theme }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" :label="$t('table.actions')" min-width="100px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary"  @click="handleUpdate(scope.row)">{{$t('table.edit')}}</el-button>
          <el-button type="primary"  @click="setFees(scope.row)">{{$t('course.setFees')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

     <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="35%" style='margin-top:-3%;'>
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="120px" style='margin-left:20px;'>
        <el-form-item :label="$t('course.name')" prop="name">
          <el-input v-model="temp.name"></el-input>
        </el-form-item>
        <el-form-item :label="$t('course.theme')" prop="theme">
          <el-input v-model="temp.theme"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{$t('table.cancel')}}</el-button>
        <el-button type="primary" @click="updateData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="$t('course.setFees')" :visible.sync="feeShow" :width="activeId === 100001 || activeId === 100002 ? '50%' : '25%' " style='margin-top:-3%;'>
      <el-table v-if="activeId === 100001 || activeId === 100002" :key='tableKey' :data="fees" v-loading="listLoading" element-loading-text="给我一点时间" border fit >
        <el-table-column min-width="200" align="center" :label="$t('course.discipline')">
          <template slot-scope="scope">
            <span>{{ scope.row.discipline }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="activeId === 100001" min-width="200" align="center" :label="$t('course.cycle')">
          <template slot-scope="scope">
            <span>{{ scope.row.cycle }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="activeId === 100002" min-width="200" align="center" :label="$t('course.yhpy_level')">
          <template slot-scope="scope">
            <span>{{ scope.row.yhpyLevel }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="100" align="center" :label="$t('course.money')">
          <template slot-scope="scope">
              <el-input class="edit-input" size="small" v-model="scope.row.money"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <el-input v-else class="edit-input" size="small" v-model="fee"></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button @click="feeShow = false">{{$t('table.cancel')}}</el-button>
        <el-button type="primary" @click="saveFees">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { courses, update, getFees, saveFees } from '@/api/course'

export default {
  name: 'services',
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogFormVisible: false,
      dialogStatus: '',
      temp: {
        id: undefined,
        name: undefined,
        theme: undefined
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        theme: [{ required: true, message: '请输入主题', trigger: 'blur' }]
      },
      textMap: {
        update: '编辑'
      },
      feeShow: false,
      fees: [],
      fee: undefined,
      activeId: undefined
    }
  },
  filters: {
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      courses().then(response => {
        this.list = response.data.data
        this.listLoading = false
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          update(tempData).then(() => {
            for (const v of this.list) {
              if (v.id === this.temp.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.temp)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    setFees(row) {
      this.activeId = row.id
      getFees(row.id).then(res => {
        if (this.activeId === 100001 || this.activeId === 100002) {
          res.data.data.forEach(item => {
            item.money = item.money / 100
          })
          this.fees = res.data.data
        } else {
          this.fee = res.data.data[0].money / 100
        }
        this.feeShow = true
        console.log(this.fees)
      })
    },
    saveFees() {
      const params = {
        courseId: this.activeId,
        rules: []
      }
      if (this.activeId === 100001 || this.activeId === 100002) {
        this.fees.forEach(item => {
          params.rules.push({
            discipline: item.discipline,
            cycle: item.cycle,
            yhpyLevel: item.yhpyLevel,
            money: item.money * 100
          })
        })
      } else {
        params.rules.push({
          discipline: undefined,
          cycle: undefined,
          yhpyLevel: undefined,
          money: this.fee * 100
        })
      }
      // saveFees(JSON.stringify(params)).then(res => {
      //   this.feeShow = false
      // })
      var formData = new FormData()
      formData.append('rules', JSON.stringify(params))
      saveFees(formData).then(res => {
        this.feeShow = false
      })
    }
  }
}
</script>
